// pages/my_stylists/my_stylists.js
const app = getApp() // 引入app
const _request = require("../../utils/request.js"); // 引入require;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    queryStylistName: "",
    page: 1,
    sign: 0,
    enter: 0,
    hairStylist: [],
    nexus: 0,
    shopimgUrl: app.globalData.shopimgUrl
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    this.setData({
      nexus:0,
      page:1,
      hairStylist: []
    })
    this.getStoreStylistNumber();
  },

  onShow: function () {
    this.queryStylist();
  },
  // 美发师数量
  getStoreStylistNumber() {
    _request.postObj('/store/getStoreStylistNumber', {
      storeId: wx.getStorageSync('storeId'),
      nickname: this.data.queryStylistName,
    }).then((res) => {
        if (res.data.code === 200) {
          this.setData({
            sign: res.data.data.sign,
            enter: res.data.data.enter
          })
        }
      }

    ).catch((error) => {
      console.log(error, 'error');
    })
  },
  // 店内，平台切换
  queryListByType(event) {
    this.setData({
      nexus:event.detail.index,
      page:1,
      hairStylist: []
    })
    this.queryStylist();
  },
  // 清空
  onClear:function(){
    this.setData({
      hairStylist: [],
      page:1,
      queryStylistName:''
    })
    this.queryStylist();
  },
   // 搜索
  onQuery: function () {
    this.setData({
      page:1,
      hairStylist: []
    })
    this.queryStylist();
    this.getStoreStylistNumber();
  },
  // 查询美发师列表
  queryStylist() {
    _request.postObj('/store/myStylist', {
      nickname: this.data.queryStylistName,
      storeId: wx.getStorageSync('storeId'),
      userId: wx.getStorageSync('userId'),
      // nexus: this.data.nexus,
      page: this.data.page
    }).then((res) => {
      if (res.data.code === 200) {
        let list = res.data.data.map(item => {
          let distance = (parseInt(item.distance) / 1000).toFixed(1);
          item.newdistance = Math.round(distance * 10) / 10
          if (item.lable) {
            let money = item.lable.match(/\d+(.\d+)?/g).join('');
            let meal = item.lable.split(money)[0]
            return {
              ...item,
              money,
              meal,
            }
          } else {
            return {
              ...item
            }
          }
        })
        // console.log(list)
        this.setData({
          hairStylist: this.data.page > 1 ? [...this.data.hairStylist, ...list] : list
        })
      }
    })
  },
  // 详情
  onQueryDetail(e) {
    const {
      item
    } = e.currentTarget.dataset
    console.log(item);
    let obj = JSON.stringify(item)
    wx.navigateTo({
      url: '/pages/hairstylistdetails/index?hair=' + encodeURIComponent(obj) + '&status=2',
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      page: this.data.page + 1
    })
    this.queryStylist();
  },
})